package com.xbongbong.paas.service.workflow.setting.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.config.enums.WorkflowTriggerMethodEnum;
import com.xbongbong.paas.domain.entity.ext.OptionalRangeEntity;
import com.xbongbong.paas.help.WorkflowNodeHelp;
import com.xbongbong.paas.pojo.workflow.NodeCoreConfigurationPojo;
import com.xbongbong.paas.pojo.workflow.WorkflowNodeTreePojo;
import com.xbongbong.paas.pojo.workflow.nodes.vo.WorkflowNodeGetVO;
import com.xbongbong.paas.pojo.workflow.settings.WorkflowGetPojo;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowEnableVO;
import com.xbongbong.paas.service.workflow.setting.AbstractWorkflowNodeSettings;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.WorkflowNodeUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.domain.entity.ext.WorkflowNodeEntityExt;
import com.xbongbong.workflow.enums.WorkflowNodeTypeEnum;
import com.xbongbong.workflow.pojo.nodes.ApprovalNode;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/05/21 14:35
 */
@Service("workflowNodeApproveSettings")
public class WorkflowNodeApproveSettings extends AbstractWorkflowNodeSettings {


    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowNodeApproveSettings.class);
    @Resource
    private WorkflowNodeHelp workflowNodeHelp;

    @Override
    public Integer type() {
        return WorkflowNodeTypeEnum.APPROVE_NODE.getType();
    }


    @Override
    public WorkflowNodeTreePojo getContent(WorkflowNodeEntity workflowNodeEntity, WorkflowGetPojo workflowGetPojo) throws XbbException {
        WorkflowNodeTreePojo workflowNodeTreePojo = new WorkflowNodeTreePojo();
        try {
            ApprovalNode approvalNode = JSON.parseObject(workflowNodeEntity.getConfigData(), ApprovalNode.class);
            if (Objects.isNull(approvalNode) || CollectionUtils.isEmpty(approvalNode.getMainUserList())) {
                return workflowNodeTreePojo;
            }
            List<OptionalRangeEntity> optionalRangeList = approvalNode.getMainUserList();

            workflowNodeTreePojo = getNameAndAvatar(optionalRangeList, workflowGetPojo, workflowNodeEntity.getType());
            return workflowNodeTreePojo;

        } catch (Exception e) {
            LOGGER.error("WorkflowNodeApproveSettings.getContent error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }
    @Override
    public WorkflowNodeGetVO get(WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        try {
            ApprovalNode approvalNode = JSON.parseObject(workflowNodeEntity.getConfigData(), ApprovalNode.class);
            NodeCoreConfigurationPojo nodeCoreConfigurationPojo = new NodeCoreConfigurationPojo();
            String corpid = workflowNodeEntity.getCorpid();
            List<WorkflowNodeEntity> workflowNodeList = new ArrayList<>();
            workflowNodeList.add(workflowNodeEntity);
            WorkflowGetPojo workflowGetPojo = workflowNodeHelp.getContent(workflowNodeList, corpid);
            // 同步下审批人名称
            setOptionRangeList(workflowGetPojo, approvalNode.getMainUserList());
            // 同步下抄送人人名称
            setOptionRangeList(workflowGetPojo, approvalNode.getCcUserList());

            addFieldPermission(workflowNodeEntity, approvalNode.getFieldPermission());

            nodeCoreConfigurationPojo.setApprovalNode(approvalNode);
            return new WorkflowNodeGetVO(workflowNodeEntity.getAppId(), workflowNodeEntity.getFormId(), workflowNodeEntity.getMenuId(), workflowNodeEntity.getSaasMark(), workflowNodeEntity.getBusinessType(),
                    workflowNodeEntity.getUid(), workflowNodeEntity.getId(), workflowNodeEntity.getPrevId(), workflowNodeEntity.getWrapperId(), nodeCoreConfigurationPojo);
        } catch (Exception e) {
            LOGGER.error("WorkflowNodeApproveSettings.get error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public WorkflowEnableVO check(WorkflowNodeEntity workflowNodeEntity, Map<Long, WorkflowNodeEntity> nodeMap, WorkflowGetPojo workflowGetPojo) throws XbbException {
        WorkflowEnableVO workflowEnableVO = new WorkflowEnableVO();
        ApprovalNode approvalNode = JSON.parseObject(workflowNodeEntity.getConfigData(), ApprovalNode.class);
        if (Objects.equals(workflowNodeEntity.getTriggerType(), WorkflowTriggerMethodEnum.FORM.getType())) {
            if (CollectionUtils.isEmpty(approvalNode.getMainUserList())) {
                workflowEnableVO.setSuccess(0);
                workflowEnableVO.setWorkflowNodeId(workflowNodeEntity.getId());
            }
        }
        return workflowEnableVO;
    }

    @Override
    public void replaceWorkflowNodeId(WorkflowNodeEntityExt workflowNodeEntityExt, Map<Long, Long> workflowNodeMap) throws XbbException {
        ApprovalNode approvalNode = JSON.parseObject(workflowNodeEntityExt.getConfigData(), ApprovalNode.class);
        if (Objects.nonNull(approvalNode) && CollectionUtils.isNotEmpty(approvalNode.getTitle())) {
            WorkflowNodeUtil.titleReplaceNodeId( approvalNode.getTitle(), workflowNodeMap);
            workflowNodeEntityExt.setConfigData(JSON.toJSONString(approvalNode));
        }
    }
}

